﻿using System;
using System.Collections.Generic;

namespace Editor.Utilities
{
    public struct KeyValuePair<K, V>
    {
        private K key;
        private V value;

        public K Key
        {
            get { return key; }
            set { key = value; }
        }

        public V Value
        {
            get { return value; }
            set { this.value = value; }
        }

        public KeyValuePair(K key, V value)
        {
            this.key = key;
            this.value = value;
        }
    }

    public class SDictionary<T> : IEnumerable<KeyValuePair<string, T>>, ICloneable
    {
        public List<KeyValuePair<string, T>> ListOfPairs { get; set; }

        public int Count
        {
            get
            {
                return ListOfPairs.Count;
            }
        }

        public T this[string language]
        {
            get
            {
                T foundString = default(T);

                foreach (var pair in ListOfPairs)
                    if (pair.Key.Equals(language))
                    {
                        foundString = pair.Value;
                        break;
                    }

                return foundString;
            }
            set
            {
                for (int i = 0; i < ListOfPairs.Count; ++i)
                {
                    if (ListOfPairs[i].Key.Equals(language))
                    {
                        ListOfPairs[i] = new KeyValuePair<string, T>(language, value);

                        return;
                    }
                }

                Add(language, value);
            }
        }

        public SDictionary()
        {
            ListOfPairs = new List<KeyValuePair<string, T>>();
        }

        public void Clear()
        {
            ListOfPairs.Clear();
        }

        public void Add(Object fix)
        {
            var pair = (KeyValuePair<string, T>) fix;

            Add(pair.Key, pair.Value);
        }

        public void Add(string language, T text)
        {
            ListOfPairs.Add(new KeyValuePair<string,
                        T>(language, text));
        }

        public bool Remove(string language)
        {
            foreach (var pair in ListOfPairs)
                if (pair.Key.Equals(language))
                {
                    ListOfPairs.Remove(pair);
                    return true;
                }

            return false;
        }

        public bool ContainsLanguage(string language)
        {
            foreach (var pair in ListOfPairs)
            {
                if (pair.Key == language)
                {
                    return true;
                }
            }

            return false;
        }

        public bool ContainsValue(T value)
        {
            foreach (var pair in ListOfPairs)
            {
                if (pair.Value.Equals(value))
                {
                    return true;
                }
            }

            return false;
        }

        public bool ContainsPair(string language, T value)
        {
            foreach (var pair in ListOfPairs)
            {
                if (pair.Key == language &&
                    pair.Value.Equals(value))
                {
                    return true;
                }
            }

            return false;
        }

        public IEnumerator<KeyValuePair<string, T>> GetEnumerator()
        {
            return ListOfPairs.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ListOfPairs.GetEnumerator();
        }

        public object Clone()
        {
            SDictionary<T> dict = new SDictionary<T>();

            foreach (var pair in ListOfPairs)
                dict[pair.Key] = pair.Value;

            return dict;
        }
    }
}
